package cn.jh.clearing.repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import cn.jh.clearing.pojo.ProfitRecord;

@Repository
public interface ProfitRecordRepository extends JpaRepository<ProfitRecord,String>,JpaSpecificationExecutor<ProfitRecord>{

	
	@Query("select profitRecord from  ProfitRecord profitRecord where profitRecord.acquserid=:userid")
	Page<ProfitRecord> findProfitByUserid(@Param("userid") long userid, Pageable pageAble);
	
	@Query("select profitRecord from  ProfitRecord profitRecord where profitRecord.acquserid in (:uids)")
	Page<ProfitRecord> findProfitByBrandId(@Param("uids") Long[] uids, Pageable pageAble);
	
	
	@Query("select profitRecord from  ProfitRecord profitRecord where profitRecord.acquserid=:userid and  profitRecord.createTime >=:startTime and profitRecord.createTime <:endTime")
	Page<ProfitRecord> findAllProfitRecord(@Param("userid") long userid, @Param("startTime") Date startTime, @Param("endTime") Date endTime, Pageable pageAble);
	
	
	@Query("select profitRecord from  ProfitRecord profitRecord where profitRecord.acquserid=:userid and  profitRecord.createTime >=:startTime")
	Page<ProfitRecord> findAllProfitRecord(@Param("userid") long userid, @Param("startTime") Date startTime, Pageable pageAble);
	
	
	@Query("select profitRecord from  ProfitRecord profitRecord where profitRecord.ordercode=:ordercode and  profitRecord.type =:type")
	ProfitRecord findProfitRecordByordercodeAndtype(@Param("ordercode") String ordercode, @Param("type") String type);
	
	@Query("select sum(acqAmount)  from  ProfitRecord profitRecord where profitRecord.acquserid=:userid")
	BigDecimal findsumProfitRecord(@Param("userid") long acquserid);
	
	//获取当前phone的数据库记录(oriphone为需要查询的手机号)
	@Query("from  ProfitRecord profitRecord where profitRecord.oriphone=:oriphone and profitRecord.ordercode=:ordercode")
	List<ProfitRecord> queryProfitByOriPhone(@Param("oriphone") String oriphone, @Param("ordercode") String ordercode);
	
	//依据ordercode获取分润表中的订单信息
	@Query("from  ProfitRecord profitRecord where profitRecord.ordercode=:ordercode")
	List<ProfitRecord> queryProfitAmount(@Param("ordercode") String ordercode);

	@Query("select profitRecord from ProfitRecord profitRecord where profitRecord.ordercode=:orderCode and profitRecord.createTime>=:startTimeDate and profitRecord.createTime<=:endTimeDate")
	Page<ProfitRecord> finByOrderCode(@Param("orderCode") String orderCode, @Param("startTimeDate") Date startTimeDate, @Param("endTimeDate") Date endTimeDate, Pageable pageable);

	@Query("select acquserid,sum(acqAmount)  from  ProfitRecord profitRecord where profitRecord.acquserid in :acqUserIds group by profitRecord.acquserid")
	List<Object[]> findsumProfitRecordByAcqUserIds(@Param("acqUserIds") long[] acqUserIds);
}
